package it.rossignoli.enrico.app.Tesi;

import java.util.List;

import de.dfki.ccaal.gestures.IGestureRecognitionListener;
import de.dfki.ccaal.gestures.IGestureRecognitionService;
import de.dfki.ccaal.gestures.classifier.Distribution;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;





public class ProvaTrainingActivity extends Activity 
{
	
	IGestureRecognitionService recognitionService;
	String activeTrainingSet;
	String activeTrainingGesto;
	boolean Riconoscimento;

	private final ServiceConnection serviceConnection = new ServiceConnection() 
	{

		
		public void onServiceConnected(ComponentName className, IBinder service) 
		{
			recognitionService = IGestureRecognitionService.Stub.asInterface(service);
			try 
			{
				//Faccio partire in modalita riconoscimento e attivo pure il recorder
				//recognitionService.startClassificationMode("default");
				//Registro il listener in maniera tale da poter recepire le informazioni inviate dal servizio sfruttando i metodi dell interfaccia listener
				//Posso quindi usare i metodi implementati nello stub di questa activity
				recognitionService.registerListener(IGestureRecognitionListener.Stub.asInterface(gestureListenerStub));
			} 
			catch (RemoteException e1) 
			{
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}

		
		public void onServiceDisconnected(ComponentName className) 
		{
			recognitionService = null;
		}
	};

	IBinder gestureListenerStub = new IGestureRecognitionListener.Stub() 
	{

		
		public void onGestureLearned(final String gestureName) throws RemoteException 
		{
			
			System.err.println("Gesture %s learned");
			runOnUiThread(new Runnable() 
			{
				
				public void run() 
				{
					Toast.makeText(ProvaTrainingActivity.this, String.format("%s:",gestureName), Toast.LENGTH_LONG).show();
					
				}
			});
		}

		
		public void onTrainingSetDeleted(String trainingSet) throws RemoteException 
		{
			Toast.makeText(ProvaTrainingActivity.this, String.format("Training set %s deleted", trainingSet), Toast.LENGTH_SHORT).show();
			System.err.println(String.format("Training set %s deleted", trainingSet));
		}

		
		public void onGestureRecognized(final Distribution distribution) throws RemoteException 
		{
			runOnUiThread(new Runnable() 
			{
				
				public void run() 
				{
					Toast.makeText(ProvaTrainingActivity.this, String.format("%s: %f", distribution.getBestMatch(), distribution.getBestDistance()), Toast.LENGTH_LONG).show();
					System.err.println(String.format("%s: %f", distribution.getBestMatch(), distribution.getBestDistance()));
				}
			});
		}
	};
	
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main1);

        
        Button Start =(Button)findViewById(R.id.StartRiconosci);
        Button startTraining = (Button)findViewById(R.id.StartTraining);
        Button stopTraining = (Button)findViewById(R.id.StopTraining);
        
        Button lista = (Button)findViewById(R.id.lista);
        
        final EditText GestoTxt =(EditText)findViewById(R.id.SetGesto);
        final EditText SetTxt =(EditText)findViewById(R.id.setSet);
        activeTrainingSet = SetTxt.getText().toString();
        activeTrainingGesto = GestoTxt.getText().toString();
        
        Start.setOnTouchListener( new OnTouchListener() 
		{
			
			public boolean onTouch(View v, MotionEvent event) 
			{
				
				// TODO Auto-generated method stub
				if(event.getAction()==MotionEvent.ACTION_DOWN || event.getAction()==MotionEvent.ACTION_MOVE)
				{
					//Inizio gesto
					//avvio lettura dati dal sensore e scrittura su un file di log
					Riconoscimento=true;
					String set=SetTxt.getText().toString();

					
					try 
					{
						recognitionService.startClassificationMode(set);
					} 
					catch (RemoteException e) 
					{
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				else if(event.getAction()==MotionEvent.ACTION_UP)
				{
					Riconoscimento=false;
					try 
					{
						recognitionService.stopClassificationMode();
						
					} 
					catch (RemoteException e) 
					{
  					// TODO Auto-generated catch block
					e.printStackTrace();
					}
					

				}
				return false;
			}
		});
        
        
        startTraining.setOnClickListener(new OnClickListener() 
        {
			
			public void onClick(View v) 
			{
				// TODO Auto-generated method stub
				if (recognitionService != null) {
					try {
						if (!recognitionService.isLearning()) {

							recognitionService.startLearnMode(SetTxt.getText().toString(),GestoTxt.getText().toString());
						} else {

							recognitionService.stopLearnMode();
						}
					} catch (RemoteException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
		});
	
        
        stopTraining.setOnClickListener(new OnClickListener() 
        {
			
			public void onClick(View v) 
			{
				// TODO Auto-generated method stub
				try 
				{
					recognitionService.stopLearnMode();
				} catch (RemoteException e) 
				{
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}
		});
        
        lista.setOnClickListener(new OnClickListener() 
        {
			
			public void onClick(View v) 
			{
				// TODO Auto-generated method stub
				
				try 
				{
					
					List<String> Item= recognitionService.getGestureList(SetTxt.getText().toString());
					//GestoTxt.setText("ok");
					
				} 
				catch (RemoteException e) 
				{
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}
		});
        
    }
    
    @Override
	protected void onRestart() 
	{
		super.onRestart();
		Log.i("ActivityDemo", "Richiamato onRestart()");
	}
	
	@Override
	protected void onStart() 
	{
		super.onStart();
		Log.i("ActivityDemo", "Richiamato onStart()");
	}
	
	@Override
	protected void onResume() 
	{
		
		Log.i("ActivityDemo", "Richiamato onResume()");
		Intent bindIntent = new Intent("de.dfki.ccaal.gestures.GESTURE_RECOGNIZER");
		bindService(bindIntent, serviceConnection, Context.BIND_AUTO_CREATE);
		super.onResume();
	}
	
	@Override
	protected void onPause() 
	{
		
		Log.i("ActivityDemo", "Richiamato onPause()");
		try 
		{
			recognitionService.unregisterListener(IGestureRecognitionListener.Stub.asInterface(gestureListenerStub));
		} catch (RemoteException e) 
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		recognitionService = null;
		unbindService(serviceConnection);
		super.onPause();
	}
	
	@Override
	protected void onStop() 
	{
		super.onStop();
		Log.i("ActivityDemo", "Richiamato onStop()");
	}
	
	@Override
	protected void onDestroy() 
	{
		super.onDestroy();
		Log.i("ActivityDemo", "Richiamato onDestroy()");
	}
}